package com.information.controller;

import com.backend.service.StatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RestController
@RequestMapping("/api/statistics")
public class StatisticsController {

    private final StatisticsService statisticsService;

    @Autowired
    public StatisticsController(StatisticsService statisticsService) {
        this.statisticsService = statisticsService;
    }

    /**
     * 获取用户概览统计
     */
    @GetMapping("/overview")
    public ResponseEntity<Map<String, Object>> getUserOverview(HttpServletRequest request) {
        try {
            Map<String, Object> overview = statisticsService.getUserOverview();
            return ResponseEntity.ok(overview);
        } catch (Exception e) {
            System.err.println("获取用户概览统计失败: " + e.getMessage());
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取地区分布统计
     */
    @GetMapping("/region")
    public ResponseEntity<Map<String, Object>> getRegionStatistics(HttpServletRequest request) {
        try {
            Map<String, Object> regionStats = statisticsService.getRegionStatistics();
            return ResponseEntity.ok(regionStats);
        } catch (Exception e) {
            System.err.println("获取地区分布统计失败: " + e.getMessage());
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取年龄分布统计
     */
    @GetMapping("/age")
    public ResponseEntity<Map<String, Object>> getAgeStatistics(HttpServletRequest request) {
        try {
            Map<String, Object> ageStats = statisticsService.getAgeStatistics();
            return ResponseEntity.ok(ageStats);
        } catch (Exception e) {
            System.err.println("获取年龄分布统计失败: " + e.getMessage());
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取账号状态统计
     */
    @GetMapping("/status")
    public ResponseEntity<Map<String, Object>> getStatusStatistics(HttpServletRequest request) {
        try {
            Map<String, Object> statusStats = statisticsService.getStatusStatistics();
            return ResponseEntity.ok(statusStats);
        } catch (Exception e) {
            System.err.println("获取账号状态统计失败: " + e.getMessage());
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取性别统计
     */
    @GetMapping("/gender")
    public ResponseEntity<Map<String, Object>> getGenderStatistics(HttpServletRequest request) {
        try {
            Map<String, Object> genderStats = statisticsService.getGenderStatistics();
            return ResponseEntity.ok(genderStats);
        } catch (Exception e) {
            System.err.println("获取性别统计失败: " + e.getMessage());
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取角色分布统计
     */
    @GetMapping("/role")
    public ResponseEntity<Map<String, Object>> getRoleStatistics(HttpServletRequest request) {
        try {
            Map<String, Object> roleStats = statisticsService.getRoleStatistics();
            return ResponseEntity.ok(roleStats);
        } catch (Exception e) {
            System.err.println("获取角色分布统计失败: " + e.getMessage());
            return ResponseEntity.status(500).build();
        }
    }

    /**
     * 获取用户注册趋势（最近30天）
     */
    @GetMapping("/trend")
    public ResponseEntity<Map<String, Object>> getRegistrationTrend(HttpServletRequest request) {
        try {
            Map<String, Object> trendStats = statisticsService.getRegistrationTrend();
            return ResponseEntity.ok(trendStats);
        } catch (Exception e) {
            System.err.println("获取注册趋势统计失败: " + e.getMessage());
            return ResponseEntity.status(500).build();
        }
    }
}